<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="100px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && editFlag"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属公司" :rules="rules.validateRequire" prop="orgname">
            <el-input v-model="model.orgname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属部门" :rules="rules.validateRequire" prop="deptname">
            <el-input v-model="model.deptname"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="相关方姓名" :rules="rules.validateRequire" prop="name">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="相关方角色">
            <el-input v-model="model.role"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="职位" prop="position">
            <el-input v-model="model.position"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="分管工作" prop="responsibilities">
            <el-input v-model="model.responsibilities" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="办公电话" prop="officephone">
            <el-input v-model="model.officephone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="移动电话" prop="mobliephone">
            <el-input v-model="model.mobliephone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮件地址" prop="mailaddress">
            <el-input v-model="model.mailaddress"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="沟通情况" prop="communication">
            <el-input v-model="model.communication" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false"> <svg-icon icon-class="save"/><span style="margin-left:2px">保存并关闭</span></el-button>
      <el-button type="primary" @click="saveAndCopy" :loading="saveLoading" v-if="disableEditSubmit === false" icon="el-icon-copy-document">保存并复制</el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true" :externalQueryParam="{ qry_processflag: '1' }"></tlpm-project-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
/** 子组件 */
import tlpmProjectRef from "@/views/ProjectManage/pmProject/refList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlpmProjectRef },
  data() {
    return {
      moduleTitle: "相关方识别",
      urlPrefix: "/pm/stakeholder/",
      url: {
        // 额外的url
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ""
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.model, "projectid", id)
      this.$set(this.model, "dispprojectid", name)
    },
    copyObj(obj) {
      const copyObj = Object.assign({}, obj)
      const { projectid, dispprojectid, orgname, deptname } = copyObj
      this.model = {}
      this.$set(this.model, "projectid", projectid)
      this.$set(this.model, "dispprojectid", dispprojectid)
      this.$set(this.model, "orgname", orgname)
      this.$set(this.model, "deptname", deptname)
    },
    saveAndCopy() {
      if (!this.beforeSubmit()) {
        return
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isAdd) {
            this.doAddNoCloseDialog()
          } else {
            this.doEditNoCloseDialog()
          }
        } else {
          return false
        }
      })
    },
    /** 执行新增提交 */
    doAddNoCloseDialog() {
      this.saveLoading = true
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.$emit('refresh')
            this.copyObj(this.model)
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    /** 执行编辑提交 */
    doEditNoCloseDialog() {
      this.saveLoading = true
      putAction(this.url.edit, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message(res.message, 'success')
            this.$emit('refresh')
            this.isAdd = true
            this.copyObj(this.model)
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    }
  }
}
</script>
